package net.dipper.tankwar.game.control;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import net.dipper.tankwar.core.config.BackgroundItem;
import net.dipper.tankwar.core.config.ConfigManager;
import net.dipper.tankwar.core.config.ObjectItem;
import net.dipper.tankwar.core.config.TowerItem;
import net.dipper.tankwar.core.config.UnitItem;
import net.dipper.tankwar.core.data.ObjectMap;
import net.dipper.tankwar.core.helper.Position;
import wnb.util.control.Command;

public class RunUnitCommand extends Command {
	private static final int SHOOT_RATE = 2;
	private static final int MAX_OPERATION_COUNTER = 20;
	private int operationCounter;
	private ObjectMap map;
	
	public RunUnitCommand(ObjectMap map){
		this.map = map;
	}

	@Override
	public void exec() {
		try {
				if(operationCounter > 0){
						operationCounter--;
						return;
				}
				
				for(int i=0;i<map.getWidth();i++){
					for(int j=0;j<map.getHeight();j++){
						byte value = map.getObject(i, j);
						if(value == 0)
							continue;
						
					ConfigManager manager = ConfigManager.getInstance();
					ObjectItem item = (ObjectItem) manager.getObject(value);
					Position player = map.getPlayer();
					if(i == player.x && j == player.y)
						continue;
					
					int code = (int) (SHOOT_RATE * Math.random());
					if(!(item instanceof UnitItem) && !(item instanceof TowerItem))
						continue;
					
					if(code == 0){
						map.shoot(i, j, getAtk(item), getRadius(item));
					}else{
						int offsetX = player.x - i;
						int offsetY = player.y - j;
						int x = i;
						int y = j;
						byte direction = ObjectMap.DIRECTION_N;
						if(Math.abs(offsetX) > Math.abs(offsetY)){
							if(offsetX > 0){
								x += 1;
								direction = ObjectMap.DIRECTION_E;
							}else{
								x -= 1;
								direction = ObjectMap.DIRECTION_W;
							}
						}else{
							if(offsetY > 0){
								y += 1;
								direction = ObjectMap.DIRECTION_S;
							}else{
								y -= 1;
								direction = ObjectMap.DIRECTION_N;
							}
						}
						
						if(direction != map.getDirection(i, j)){
							map.turn(i, j, direction);
						}else if(item instanceof UnitItem){
							byte backgroundVal = map.getBackground(x, y);
							BackgroundItem background = (BackgroundItem) manager.getBackground(backgroundVal);
							if(background == null || !background.getIsCrossable())
								continue;
						
							map.move(i, j, x, y, direction);
						}
					}
					
				}
			}
			
			operationCounter = MAX_OPERATION_COUNTER;
			} catch (ClassNotFoundException | InstantiationException
					| IllegalAccessException | ParserConfigurationException
					| SAXException | IOException e) {
				e.printStackTrace();
			}
	}

	//获取射击半径
	private double getRadius(ObjectItem item) {
		if(item instanceof TowerItem){
			return ((TowerItem) item).getRadius();
		}else if(item instanceof UnitItem){
			return ((UnitItem) item).getRadius();
		}else{
			return 0;
		}
	}

	//获取攻击力
	private int getAtk(ObjectItem item) {
		if(item instanceof TowerItem){
			return ((TowerItem) item).getAtk();
		}else if(item instanceof UnitItem){
			return ((UnitItem) item).getAtk();
		}else{
			return 0;
		}
	}

}
